package com.xneure.utils.intf;

import java.io.IOException;
import java.lang.reflect.Type;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;

public abstract class JsonHandler implements ObjectSerializer,ObjectDeserializer{
	@SuppressWarnings("unchecked")
	@Override
	public <T> T deserialze(DefaultJSONParser parser,Type type,Object fieldName){
		return (T)toObject(parser.parse(),type,fieldName);
	}

	@Override
	public int getFastMatchToken(){
		return JSONToken.LITERAL_STRING;
	}

	@Override
	public void write(JSONSerializer serializer,Object object,Object fieldName,Type fieldType,int features) throws IOException{
		serializer.getWriter().write("\"".concat(toJson(object,fieldName,fieldType)).concat("\""));
	}

	public abstract Object toObject(Object obj,Type type,Object fieldName);

	public abstract String toJson(Object object,Object fieldName,Type fieldType);
}
